package cn.iocoder.yudao.module.erp.controller.app.product;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryListReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpAppProductCategoryList2ReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpAppProductCategoryListReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpProductCategoryPageReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpProductCategoryRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "应用端 - ERP 产品分类")
@RestController
@RequestMapping("/erp/product-category")
@Validated
public class ErpAppProductCategoryController {

    @Resource
    private ErpProductCategoryService productCategoryService;

    @GetMapping("/get")
    @Operation(summary = "获得产品分类详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ErpProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
        ErpProductCategoryDO category = productCategoryService.getProductCategory(id);
        return success(BeanUtils.toBean(category, ErpProductCategoryRespVO.class));
    }

    @GetMapping("/firstList")
    @Operation(summary = "获得产品一级分类列表")
    public CommonResult<List<ErpProductCategoryRespVO>> getProductCategoryList(@Valid ErpAppProductCategoryListReqVO listReqVO) {
        List<ErpProductCategoryDO> list = productCategoryService.getAppFirstList(listReqVO);
        return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
    }

    @GetMapping("/secondList")
    @Operation(summary = "根据一级产品分类编号获得产品二级分类列表")
    public CommonResult<List<ErpProductCategoryRespVO>> getProductCategoryList(@Valid ErpAppProductCategoryList2ReqVO listReqVO) {
        List<ErpProductCategoryDO> list = productCategoryService.getAppSecondList(listReqVO);
        return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
    }


    @GetMapping("/firstPage")
    @Operation(summary = "获得产品一级分类列表分页")
    public CommonResult<PageResult<ErpProductCategoryRespVO>> getProductCategoryPage(@Valid ErpProductCategoryPageReqVO pageReqVO) {
        PageResult<ErpProductCategoryDO> productCategoryPage = productCategoryService.getProductCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(productCategoryPage, ErpProductCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品分类列表")
    public CommonResult<List<ErpProductCategoryRespVO>> getProductCategoryList(ErpProductCategoryListReqVO pageReqVO) {
        List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList(pageReqVO);
        return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
    }
}
